package com.woniu40.petai_manager.web;

import com.github.pagehelper.PageInfo;
import com.woniu40.petai_manager.entity.Manager;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.ManagerService;
import com.woniu40.petai_manager.vo.ManagerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
@Api
public class ManagerController {
    @Autowired
    ManagerService managerService;
    @GetMapping("/list")
    @ApiOperation(value = "管理员分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", required = false, dataType = "Integer", paramType = "query")
    })
    public ResponseResult<PageInfo<Manager>> list(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize",defaultValue = "4")Integer pageSize,
                                                  String userName){
        try {
            PageInfo<Manager> manager = managerService.findManagerByPage(pageNum, pageSize, userName);
            ResponseResult<PageInfo<Manager>> result = new ResponseResult<>();
            result.setCode(101);
            result.setMessage("查询成功");
            result.setData(manager);
            return result;
        }catch (RuntimeException e){
            e.printStackTrace();
            ResponseResult<PageInfo<Manager>> result=new ResponseResult<>();
            result.setCode(104);
            result.setMessage("查询失败");
            return result;
        }
    }
    @PostMapping("/addManager")
    @ResponseBody
    @ApiOperation(value = "添加管理员")
    public ResponseResult<ManagerVo> addManager(@RequestBody ManagerVo managerVo){
        System.out.println(managerVo);
        //对选中角色去重
        List<Integer> rolelist = managerVo.getRolelist();
        Set<Integer> set =new HashSet<>();
        for (Integer integer :rolelist) {
            set.add(integer);

        }
        List<Integer> roleIds=new ArrayList<>();
        for (Integer integer :set) {
            roleIds.add(integer);

        }
        managerVo.setRolelist(roleIds);
        Date date=new Date();
        managerVo.setInsertTime(date);
        managerVo.setUpdateTime(date);
         int i = managerService.addManager(managerVo);
        /*Integer userCode = managerVo.getUserCode();*/
//            根据前台传回来的userCode查询出刚刚保存的管理员数据
        /*Manager manager = managerService.findByPrimaryKey(userCode);
        Integer managerId = manager.getManagerId();
        managerVo.setManagerId(managerId);*/
        if (i==1){
            ResponseResult<ManagerVo> result=new ResponseResult<>();
            result.setCode(203);
            result.setMessage("添加成功");
            result.setData(managerVo);
            return result;
        }else {
            ResponseResult<ManagerVo> result=new ResponseResult<>();
            result.setCode(104);
            result.setMessage("添加失败");

            return result;
        }
    }
    @DeleteMapping("/removeManager/{managerId}")
    @ApiOperation(value = "删除管理员的方法")
    public ResponseResult<Manager> removeManager(@PathVariable(value = "managerId") Integer managerId){
        int i = managerService.deleteManager(managerId);

        if (i==1){
            ResponseResult<Manager> result=new ResponseResult<>();
            result.setCode(208);
            result.setMessage("删除成功");
            return result;
        }else {
            ResponseResult<Manager> result=new ResponseResult<>();
            result.setCode(108);
            result.setMessage("删除失败");
            return result;

        }

    }
    @GetMapping("/findManagerVo")
    @ApiOperation(value = "查询管理员及其角色")
    public ResponseResult<ManagerVo> findManagerVo(Integer managerId) {
        System.out.println(managerId);
        //ManagerVo managerVo = managerService.findManagerVo(managerId);
        ManagerVo managerVo = managerService.findManagerById(managerId);
        //根据管理员ID查询中间表的角色ID 手动set到managerVo中
        List<Integer> rolelist = managerService.findRoleByMid(managerId);
        managerVo.setRolelist(rolelist);
        /*System.out.println(managerId);*/
        ResponseResult<ManagerVo> result = new ResponseResult<>();
        if (managerVo != null) {
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(managerVo);
        } else {
            result.setCode(107);
            result.setMessage("查询失败");
        }
        return result;
    }
    @PutMapping("/modifyManager")
    @ApiOperation(value = "修改管理员")
    public ResponseResult<ManagerVo> modifyManager(@RequestBody ManagerVo managerVo){
        System.out.println(managerVo);
        List<Integer> rolelist = managerVo.getRolelist();
        Set<Integer> set = new HashSet<>();
        for (Integer integer : rolelist) {
            set.add(integer);
        }
        List<Integer> roleIds = new ArrayList<>();
        for (Integer integer : set) {
            roleIds.add(integer);
        }
        managerVo.setRolelist(roleIds);

        managerVo.setUpdateTime(new Date());
        System.out.println(managerVo);
        int i = managerService.modifyManagerAndRole(managerVo);

        if (i==1){
            ResponseResult<ManagerVo> result=new ResponseResult<>();
            result.setCode(203);
            result.setMessage("修改成功");
            result.setData(managerVo);
            return result;
        }else {
            ResponseResult<ManagerVo> result=new ResponseResult<>();
            result.setCode(104);
            result.setMessage("修改失败");

            return result;
        }
    }
}
